package leetcode;

/**
 * 描述:240. 搜索二维矩阵 II
 *
 * @author Madison You
 * @created 23:53
 */
public class Mid_240_SearchMatrix {
    public static void main(String[] args) {
        int[] i1 = {1,4,7,11,15};
        int[] i2 = {2,5,8,12,19};
        int[] i3 = {3,6,9,16,22};
        int[] i4 = {10,13,14,17,24};
        int[] i5 = {18,21,23,26,30};
        int[][] arr = new int[5][5];
        arr[0] = i1;
        arr[1] = i2;
        arr[2] = i3;
        arr[3] = i4;
        arr[4] = i5;
        new Mid_240_SearchMatrix().searchMatrix(arr, 5);
    }

    public boolean searchMatrix(int[][] matrix, int target) {
        boolean flag = false;
        for (int i = 0; i < matrix.length; i++) {
            int val = find(matrix[i], 0, matrix[i].length - 1, target);
            if (val != -1) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    private int find(int[] matrix, int left, int right ,int target) {
        while (left <= right) {
            int mid = (left + right) / 2;
            if (matrix[mid] == target) {
                return mid;
            }
            if (matrix[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }


}
